package com.corticon.dialogs.dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.corticon.dialogs.model.Question;
import com.corticon.dialogs.model.QuestionInSet;
import com.corticon.dialogs.model.QuestionSet;
import com.corticon.dialogs.model.ResponseEntry;
import com.corticon.dialogs.model.ResponseType;
import com.corticon.dialogs.model.UiControl;
import com.corticon.dialogs.model.User;

@Repository("questionManager")
public class QuestionManager extends HibernateDaoSupport {

    public Question getQuestionById(String QId) {
        List Lquestion = this.getHibernateTemplate().find(
                "from Question as a where a.questionId=" + QId);
        if (Lquestion != null && Lquestion.size() > 0 && Lquestion.get(0) != null) {
            return (Question) Lquestion.get(0);
        }
        return null;
    }

    public List getResponseType() {
        List LresponseType = this.getHibernateTemplate().find(
                "from ResponseType as a where a.name is not null");
        if (LresponseType != null && LresponseType.size() > 0 && LresponseType.get(0) != null) {
            return LresponseType;
        }
        return null;
    }

    public UiControl getUIbyName(String Name) {
        List Lui = this.getHibernateTemplate().find(
                "from UiControl as a where a.name='" + Name + "'");
        if (Lui != null && Lui.size() > 0 && Lui.get(0) != null) {
            return (UiControl) Lui.get(0);
        }
        return null;
    }

    public List getUserByGroupId(String GroupId) {
        List Lquestion = this.getHibernateTemplate().find(
                "from User as a where a.userGroup.name='" + GroupId + "'");
        if (Lquestion != null && Lquestion.size() > 0 && Lquestion.get(0) != null) {
            return Lquestion;
        }
        return null;
    }

    public ResponseType getResponsetype(String Id) {
        List Ltype = this.getHibernateTemplate().find(
                "from ResponseType as a where a.responseTypeId=" + Id + " order by a.responseTypeId");
        if (Ltype != null && Ltype.size() > 0 && Ltype.get(0) != null) {
            return (ResponseType) Ltype.get(0);
        }
        return null;
    }

    public String getResponsetEntity(String Responsetype, boolean IsCheck) {
        String OutHtml = "";
        try {
            List Lentity = this.getHibernateTemplate().find(
                    "from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype + " order by a.sequence");
            /**
             * temple:
             * <li><strong>none</strong>000-000</li>
             * modify by heweiya 2008/11/16
             * <dd><div><strong>1 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 31 to 3</strong>
             * <em>000-000 000-000 v 000-000 000-000 v 000-000 000-000 v 000-000 000-000 v 000-000 000-000 v 000-000 000-000 v 000-000 000-000 v
             * </em></div></dd>
             *
             */
            if (Lentity.size() > 0 && Lentity.get(0) != null) {
                for (int i = 0; i < Lentity.size(); i++) {
                    ResponseEntry entry = (ResponseEntry) Lentity.get(i);
                    if (IsCheck) {
                        OutHtml += "<dd><div><strong>" + entry.getLabel() + "</strong><em>" + entry.getValue() + "</em></div></dd>";
                    } else {
                        OutHtml += "<dd id=\"Rsponse"+entry.getResponseEntryId().toString()+"\" onclick=CheckIt(this,'" + entry.getResponseEntryId().toString() +"')><div><strong>" + entry.getLabel() + "</strong><em>" + entry.getValue() + "</em></div></dd>";
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return OutHtml;
    }

    public String getMaxResponsetEntity(String Responsetype) {
        List Lentity = this.getHibernateTemplate().find(
                "select max(a.sequence) from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype);
        if (Lentity.size() > 0 && Lentity.get(0) != null) {
            return Lentity.get(0).toString();
        } else {
            return "1";
        }

    }

    public void delResponsetEntity(String EntityId) {
        List Lentity = this.getHibernateTemplate().find(
                "from ResponseEntry as a where a.responseEntryId=" + EntityId);
        if (Lentity.size() > 0 && Lentity.get(0) != null) {
            ResponseEntry entity = (ResponseEntry) Lentity.get(0);
            this.getHibernateTemplate().delete(entity);
        }
    }

    public void moveDownEntity(String Responsetype, String EntityLabel) {
        List Lthisentity = this.getHibernateTemplate().find(
                "from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype + " and a.responseEntryId=" + EntityLabel);
        if (Lthisentity.size() > 0 && Lthisentity.get(0) != null) {
            ResponseEntry entity = (ResponseEntry) Lthisentity.get(0);
            List Lentity = this.getHibernateTemplate().find(
                    "from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype + " and a.sequence>" + entity.getSequence().toString() + " order by a.sequence");
            if (Lentity.size() > 0 && Lentity.get(0) != null) {
                ResponseEntry nextentity = (ResponseEntry) Lentity.get(0);
                Long s_entity = entity.getSequence();
                Long s_nextentity = nextentity.getSequence();
                nextentity.setSequence(s_entity);
                entity.setSequence(s_nextentity);
                this.getHibernateTemplate().update(nextentity);
                this.getHibernateTemplate().update(entity);
            }
        }
    }

    public void moveUpEntity(String Responsetype, String EntityLabel) {
        List Lthisentity = this.getHibernateTemplate().find(
                "from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype + " and a.responseEntryId=" + EntityLabel);
        if (Lthisentity.size() > 0 && Lthisentity.get(0) != null) {
            ResponseEntry entity = (ResponseEntry) Lthisentity.get(0);
            List Lentity = this.getHibernateTemplate().find(
                    "from ResponseEntry as a where a.responseType.responseTypeId=" + Responsetype + " and a.sequence<" + entity.getSequence().toString() + " order by a.sequence desc");
            if (Lentity.size() > 0 && Lentity.get(0) != null) {
                ResponseEntry nextentity = (ResponseEntry) Lentity.get(0);
                Long s_entity = entity.getSequence();
                Long s_nextentity = nextentity.getSequence();
                nextentity.setSequence(s_entity);
                entity.setSequence(s_nextentity);
                this.getHibernateTemplate().update(nextentity);
                this.getHibernateTemplate().update(entity);
            }
        }
    }

    public String getNextVersionQuetion(String questionId) {
        Question question = this.getQuestionById(questionId);
        String NewVersion = "1";
        List LnextVersion = this.getHibernateTemplate().find(
                "select max(a.version) from Question as a where a.refId='" + question.getRefId() + "'");
        if (LnextVersion.size() > 0 && LnextVersion.get(0) != null) {
            NewVersion = LnextVersion.get(0).toString();
            NewVersion = String.valueOf(Integer.valueOf(NewVersion) + 1);
        }
        return NewVersion;
    }

    public void delEntity(String ResponseTypeId) {
        List Lentity = this.getHibernateTemplate().find(
                "from ResponseEntry as a where a.responseType.responseTypeId=" + ResponseTypeId);
        this.getHibernateTemplate().deleteAll(Lentity);
    }

    public List getEntity(String ResponseTypeId) {
        return this.getHibernateTemplate().find(
                "from ResponseEntry as a where a.responseType.responseTypeId=" + ResponseTypeId);
    }

    public void saveEntity(ResponseType type) {
        this.getHibernateTemplate().save(type);
    }

    public boolean findSameQuestion(String questionCode, long version) {
        List sames = this.getHibernateTemplate().find("from Question as a where a.refId='" + questionCode + "' and a.version=" + version);
        if (sames.size() > 0 && sames.get(0) != null) {
            return true;
        }
        return false;
    }

    public void copyEntity(String ResponseTypeId, ResponseType newType,
            User modifiedBy) {

        List oldEntity = this.getEntity(ResponseTypeId);
        List<ResponseEntry> newEntity = new ArrayList();
        for (int i = 0; i < oldEntity.size(); i++) {
            ResponseEntry oldEntry = (ResponseEntry) oldEntity.get(i);
            ResponseEntry newEntry = new ResponseEntry();
            newEntry.setId(oldEntry.getId());
            newEntry.setLabel(oldEntry.getLabel());
            newEntry.setModifiedBy(modifiedBy);
            newEntry.setModifiedOn(new Date());
            newEntry.setResponseType(newType);
            newEntry.setSequence(oldEntry.getSequence());
            newEntry.setValue(oldEntry.getValue());
            // saveEntity(newEntry);
            try {
                this.getHibernateTemplate().save(newEntry);
            } catch (DataAccessException e) {
                e.printStackTrace();
            }
        }
    // this.getHibernateTemplate().saveOrUpdateAll(newEntity);
    }

    public List<QuestionSet> getByQuestion(String id) {
        List<QuestionSet> questionSets = new ArrayList();
        List<QuestionInSet> questionInSets = this.getHibernateTemplate().find("from QuestionInSet as a where a.question.questionId=" + id);
        if (questionInSets.size() > 0 && questionInSets.get(0) != null) {
            for (int i = 0; i < questionInSets.size(); i++) {
                QuestionSet set = questionInSets.get(i).getQuestionSet();
                questionSets.add(set);
            }
        } else {
            return questionSets;
        }
        return questionSets;
    }
}
